﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateTable();
    }

    protected void CreateTable()
    {
        Table1.Rows.Clear();
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        Moddb08_WCF.Tables tables = frontEnd.GetAllTables();
        TableHeaderRow headerRow = new TableHeaderRow();
        TableHeaderCell checkBoxHeader = new TableHeaderCell();
        checkBoxHeader.Text = "";
        headerRow.Cells.Add(checkBoxHeader);
        Table1.Rows.Add(headerRow);
        TableHeaderCell hCell = new TableHeaderCell();
        Literal lit = new Literal();
        lit.Text = "<b>Table Name</b>";
        hCell.Controls.Add(lit);
        headerRow.Cells.Add(hCell);
        for (int i = 0; i < tables.Count; i++)
        {
            TableRow tableRow = new TableRow();
            TableCell checkbox = new TableCell();
            CheckBox cBox = new CheckBox();
            cBox.ID = i.ToString();
            checkbox.Controls.Add(cBox);
            tableRow.Cells.Add(checkbox);
            TableCell cell = new TableCell();
            Literal literal = new Literal();
            Uri tableUrl = new Uri("Tables.aspx?tableName="+tables[i].Name,UriKind.Relative);
            literal.Text = "<a href=\"" + tableUrl.ToString() +"\">"+tables[i].Name+"<a/>";
            cell.Controls.Add(literal);
            tableRow.Cells.Add(cell);
            Table1.Rows.Add(tableRow);
        }
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        int numberOfTablesDeleted = 0;
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        Moddb08_WCF.Tables tables = frontEnd.GetAllTables();
        for (int i = 0; i < tables.Count; i++)
        {
            Object o = Request.Form[i.ToString()];
            if (o != null)
            {
                string checkBox = o.ToString();
                if (checkBox.Equals("on"))
                {
                    frontEnd.DeleteTable(tables[i].Name);
                    numberOfTablesDeleted++;
                }
            }
        }
        Label1.Text = "Number of rows deletes: " + numberOfTablesDeleted.ToString();
        CreateTable();
    }
    protected void Create_Click(object sender, EventArgs e)
    {
        string tableName = TableName.Text;
        if (tableName == null || tableName.Equals(""))
        {
            Label1.Text = "You must enter a table name in the texbox";
            CreateTable();
        }
        else
        {
            ModdbFrontEnd frontEnd = new ModdbFrontEnd();
            frontEnd.CreateTable(tableName);
            Label1.Text = "Table " + tableName + "was created!";
            CreateTable();
        }
    }
}
